package com.lion.common.spring.openapi.customizers.operation;

import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import java.util.List;

/**
 * @author Chris Chan
 * Create On 2025/8/6 上午10:55
 * Use for: 删除显式声明的HttpHeader参数
 * Explain:
 * 这几个可以处理成通用的工具配置
 */
@Component
public class HttpHeaderOperationCustomizer implements OperationCustomizer {

    @Override
    public Operation customize(Operation operation, HandlerMethod method) {
        // 如果参数类型是HttpHeader，并且在参数表中，就删除
        List<Parameter> parameters = operation.getParameters();
        if (parameters != null) {
            parameters.removeIf(param ->
            {
                Schema<?> schema = param.getSchema();
                if (schema == null) {
                    return false;
                }
                String ref = schema.get$ref();
                if (ref == null) {
                    return false;
                }
                return ref.equals("#/components/schemas/HttpHeader") &&
                        param.getIn().equals(ParameterIn.QUERY.toString());
            });
        }
        return operation;
    }
}